1353D - Constructing the Array - CodeForces Solution


constructive algorithms data structures sortings *1600

Please click on ads to support us..

Python Code:

import math,sys
from collections import defaultdict,deque
import bisect as bi
def yes():print('YES')
def no():print('NO')
def I():return (int(sys.stdin.readline()))
def In():return(map(int,sys.stdin.readline().split()))
def Sn():return sys.stdin.readline().strip()
def dict(a):
    d={} 
    for x in a:
        if d.get(x,-1)!=-1:
            d[x]+=1
        else:
            d[x]=1
    return d
def find_gt(a, x):
    'Find leftmost value greater than x'
    i = bi.bisect_left(a, x)
    if i != len(a):
        return i
    else:            
        return -1
def main():
    try:
        n=I()
        from heapq import heappush , heappop
        pq = [(-n,0,n-1)]
        ans = [0]*n
        cnt = 1
        while pq and cnt<=n:
            size , start,end = heappop(pq)
            size*=-1
            ind = ((size-1)//2)+start
            ans[ind]=cnt
            if start!=ind:
                heappush(pq,(-(ind-start),start,ind-1))
            if end!=ind:
                heappush(pq,(-(end-ind),ind+1,end))
            cnt+=1
        print(*ans)


    except:
        pass
        
M = 998244353
P = 1000000007
 
if __name__ == '__main__':
    for _ in range(I()):main()
    

C++ Code:

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair<int,int>
#define iii pair<int,ii>
#define fr first
#define sc second

void solve(){
    int n,i=1;cin>>n;
    priority_queue<iii> pq;
    vector<int> a(n);
    pq.push({n,{0,-n+1}});
    while(pq.size()){
        iii top=pq.top();
        int x=top.fr,y=top.sc.fr,z=top.sc.sc;
//        cout<<x<<' '<<y<<' '<<z<<'\n';
//        system("pause");
        pq.pop();
        int mid=(-y-z)/2;
        a[mid]=i,i++;
        if(x==1)continue;
        if(mid+y>0)
            pq.push({mid+y,{y,-mid+1}});
        if(mid+z<0)
        pq.push({(mid+z)*-1,{-mid-1,z}});
    }
    for(auto i:a)cout<<i<<' ';
    cout<<'\n';
}

int32_t main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int t;cin>>t;
    while(t--)solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

1389. Create Target Array in the Given Order
1313. Decompress Run-Length Encoded List
1281. Subtract the Product and Sum of Digits of an Integer
1342. Number of Steps to Reduce a Number to Zero
1528. Shuffle String
1365. How Many Numbers Are Smaller Than the Current Number
771. Jewels and Stones
1512. Number of Good Pairs
672. Richest Customer Wealth
1470. Shuffle the Array
1431. Kids With the Greatest Number of Candies
1480. Running Sum of 1d Array
682. Baseball Game
496. Next Greater Element I
232. Implement Queue using Stacks
844. Backspace String Compare
20. Valid Parentheses
746. Min Cost Climbing Stairs
392. Is Subsequence
70. Climbing Stairs
53. Maximum Subarray
1527A. And Then There Were K
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
318. Maximum Product of Word Lengths
448. Find All Numbers Disappeared in an Array
1155. Number of Dice Rolls With Target Sum
415. Add Strings
22. Generate Parentheses
13. Roman to Integer
2. Add Two Numbers